package com.estudy.contorller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.estudy.entity.ResultVo;
import com.estudy.entity.User;
import com.estudy.service.UserService;
import com.google.common.collect.Maps;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author: yi.jianhua
 * @create: 2019/08/02 17:55
 */
@Controller
@RequestMapping("/member")
public class MemberContorller {
    private static final Logger logger = Logger.getLogger(MemberContorller.class);
    @Autowired
    private UserService userService;

    @RequestMapping("/listPage")
    public ModelAndView memmberListPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("member/member-list");
        return modelAndView;
    }

    @RequestMapping("/addPage")
    public String memberAddPage(Map<String, Object> map) {
        map.put("isAdd", true);
        return "member/member-edit";
    }

    @RequestMapping("/editPage")
    public String memberAddPage(Map<String, Object> map, String uid) {
        map.put("isAdd", false);
        User user = userService.queryUserById(uid);
        map.put("user", user);
        return "member/member-edit";
    }

    @RequestMapping("/save")
    @ResponseBody
    public String memberAdd(@ModelAttribute User user) {
        ResultVo resultVo = new ResultVo();
        try {
            boolean success = userService.addOrSave(user);
            resultVo.setStatus(200);
            resultVo.setMessage("SUCCESS");
        } catch (Exception e) {
            logger.error("memberAdd error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("保存失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }

    @RequestMapping("/del")
    @ResponseBody
    public String delUser(String id) {
        ResultVo resultVo = new ResultVo();
        try {
            boolean success = userService.del(id);
            resultVo.setStatus(200);
            resultVo.setMessage("SUCCESS");
        } catch (Exception e) {
            logger.error("delUser error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("删除失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }

    @RequestMapping("/dataList")
    @ResponseBody
    public String queryUserData() {
        try {
            Map<String, Object> map = Maps.newHashMap();
            List<User> users = userService.queryAllUsers();
            map.put("data", users);
            map.put("recordsTotal", users.size());
            map.put("recordsFiltered", users.size());
            map.put("pageNum", 1);
            return JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);
        } catch (Exception e) {
            logger.error("queryUserData error", e);
        }
        return "";
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public String updateUserStatus(String uid, int status) {
        ResultVo resultVo = new ResultVo();
        try {
            userService.updateUserStatus(uid, status);
            resultVo.setStatus(200);
            resultVo.setMessage("SUCCESS");
        } catch (Exception e) {
            logger.error("updateUserStatus error", e);
            resultVo.setStatus(500);
            resultVo.setMessage("更新失败");
        }
        return JSON.toJSONString(resultVo, SerializerFeature.WriteMapNullValue);
    }
}
